package cn.ixinjiu.libraryMangement.controller.systemAdmin.bookInfo;

import cn.ixinjiu.libraryMangement.service.IBookInfoService;
import cn.ixinjiu.libraryMangement.service.impl.BookInfoServiceImpl;
import com.alibaba.fastjson2.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: Dora.
 * @Date: 2023/05/24/12:34
 */
@WebServlet("/bookInfoDel")
public class BookInfoDelServlet extends HttpServlet {
    private IBookInfoService bookInfoService = new BookInfoServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setHeader("Content-Type", "text/html;charset=utf-8");

        JSONObject jsonObject = new JSONObject(); // 返回的json对象
        int code; // 状态码
        String msg; // 提示信息

        String id = req.getParameter("id");
        System.out.println("id = " + id);

        int i = bookInfoService.bookInfoDel(Integer.parseInt(id));
        if (i == 1) { // 删除成功
            code = 0;
            msg = "删除成功";
        }else {
            code = 1;
            msg = "删除失败";
        }

        jsonObject.put("code", code);
        jsonObject.put("msg", msg);

        PrintWriter writer = resp.getWriter();
        writer.write(jsonObject.toString());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
